/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sendtion.xrichtextdemo.utils;

import com.sendtion.xrichtextdemo.ResourceTable;
import com.sendtion.xrichtextdemo.adapter.MyPagerProvider2;
import com.zhengsr.viewpagerlib.view.BannerViewPager;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.List;

/**
 * DialogUtil
 *
 * @since 2021-03-29
 */
public class DialogUtil implements Component.ClickedListener {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    private static final int SIX = 6;
    private static final int SEVEN = 7;
    private static BannerViewPager bannerViewPager2;

    /**
     * show
     *
     * @param context
     * @param dataList
     * @param position
     */
    public static void show(Context context, List<Uri> dataList, int position) {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter
                .getInstance(context).parse(ResourceTable.Layout_image_layout, null, false);
        bannerViewPager2 = (BannerViewPager) customToastLayout.findComponentById(ResourceTable.Id_page_slider2);
        CommonDialog toastDialog = new CommonDialog(context);
        bannerViewPager2.setProvider(new MyPagerProvider2(context, dataList, bannerViewPager2, new ItemClick() {
            @Override
            public void onClick() {
                toastDialog.destroy();
            }
        }));
        bannerViewPager2.setCurrentPage(position);
        Text listsize1 = (Text) customToastLayout.findComponentById(ResourceTable.Id_listsize1);
        Text listsize3 = (Text) customToastLayout.findComponentById(ResourceTable.Id_listsize3);
        listsize1.setText(bannerViewPager2.getCurrentPage() + 1 + "");
        listsize3.setText(dataList.size() + "");
        bannerViewPager2.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
                listsize1.setText(i + 1 + "");
            }
        });

        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        customToastLayout.setLayoutConfig(layoutConfig);

        toastDialog.setContentCustomComponent(customToastLayout);
        toastDialog.setAutoClosable(true);
        toastDialog.setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }
    /**
     * ItemClick
     *
     * @since 2021-03-29
     */

    public interface ItemClick {
        /**
         * onClick
         *
         */
        void onClick();
    }

    /**
     * DialogUtil
     *
     * @param context
     */
    public DialogUtil(AbilitySlice context) {
    }

    @Override
    public void onClick(Component component) {
    }
}
